<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
function func_weather_get($city) {
	return func_weather_sina_getb($city);
}

function func_weather_sina_get($city) {
	include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.http_client.class.php');
	$http = new CoreHttpClient();
	if( $http->Connect('php.weather.sina.com.cn', 80) ) {
		$http->addHeader('Host', 'php.weather.sina.com.cn');
		$http->addHeader('User-Agent', 'PPEMI');
		$http->addHeader('Connection', 'Close');
		
		if( $http->Get('/search.php?city=' . urlencode($city)) ) {
			$body = $http->getBody();
		}
		$http->Disconnect();
		
		return $body;
	} else {
		return 'error';
	}
}

function func_weather_sina_getb($city) {
	$content = @file_get_contents('http://php.weather.sina.com.cn/search.php?city=' . ($city), 'rb');
	
	return $content;
}

function func_weather_parse_sina() {
	
}

//
function func_weather_get_list() {
	global $filecacheObj;
	//$prefix = 'weather_' . strftime("%Y%m%d", time());
	$prefix = 'weather';
	$key = 'weather';
	$cache_expiry = 3600 * 12;
	
	$list = array();
	$list = $filecacheObj->getT($key, $prefix, $cache_expiry);
	if( $filecacheObj->cache_success == true ) {
		$filecacheObj->cache_success = false;
	} else {
		include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'xml.domitlite.class.php');
		$xmldl = & new XMLDOMITLITE();
		
		$ids = array('58151', '58049', '58143', '58146', '58150', '58154', '58158', '58151');
		for($a=0, $b=count($ids); $a<$b; $a++) {
			$xmldl = & new XMLDOMITLITE();
			$string = $xmldl->readFromURL('http://www.raychou.com/weather/rss.php?id=' . $ids[$a]);
			$xmldl->parseXML($string);
			$nodes = $xmldl->documentElement->firstChild->childNodes;
			
			$district = $station = '';
			$data = array();
			$j = 0;
			for($i=0, $n=count($nodes); $i<$n; $i++) {
				$node = $nodes[$i];
				if( $node->nodeName == 'info:district' ) {
					$district = $node->getText();
				} elseif( $node->nodeName == 'info:station' ) {
					$station = $node->getText();
				} elseif( $node->nodeName == 'item' ) {
					if( $j > 1 ) {
						break;
					}
					$data[] = func_weather_filter_tt($node->firstChild->getText(), $j);
					$j++;
				}
			}
			$list[] = $station . ' ' . implode("&nbsp;&nbsp;&nbsp;&nbsp;", $data);
		}
		$filecacheObj->set($key, $list, $prefix);
	}
	
	return $list;
}

function func_weather_filter_tt($data, $j) {
	global $LANGUAGE;
	$a = explode(" ", $data);
	if( is_array($a) && count($a) > 1 ) {
		if( $j == 0 ) {
			$a['0'] = $LANGUAGE['Weather Today'];
		} elseif( $j == 1 ) {
			$a['0'] = $LANGUAGE['Weather Tomorrow'];
		}
		$data = implode(" ", $a);
	}
	
	return $data;
}

//http://www.jhxww.gov.cn/gg/tq.htm
function func_weather_get_jhxww() {
	global $filecacheObj;
	$prefix = strftime("%Y%m%d", time());
	$key = 'weather_jhxww';
	
	$s = $filecacheObj->get($key, $prefix);
	if( $filecacheObj->cache_success == true ) {
		$filecacheObj->cache_success = false;
	} else {
		$s = '';
		$content = @file_get_contents('http://www.jhxww.gov.cn/gg/tq.htm', 'rb');
		/*
		if( $p = strpos($content, "<FONT") ) {
			$a = substr($content, $p, strpos($content, "</FONT>") - $p);
			if( function_exists('mb_convert_encoding') ) {
				$s = mb_convert_encoding($a, "UTF-8", "GB2312");
			}
		}*/
		if( strlen($content) > 0 && preg_match("'<FONT[^>]*?>(.*?)</FONT>'si", $content, $matches) ) {
			$a = $matches['1'];
			if( function_exists('mb_convert_encoding') ) {
				$s = mb_convert_encoding($a, "UTF-8", "GB2312");
			}
		}
		global $LANGUAGE;
		$s = $LANGUAGE['Weather JIANHU'] . ': ' . $s;
		$filecacheObj->set($key, $s, $prefix);
	}
	
	return $s;
}
//
?>